<?php
/**
 * 邮件操作类
 * 
 * @copyright: 2008 PhpFo.Com http://www.phpfo.com
 * @license http://www.phpfo.com PhpFo.Com
 * @version  : 1.0.0
 * @package  : Mail
 * @author   : Joseph Chen (Jsph.chen@gmail.com)
 * @example  :
 * Created   : 2007-10-10
 * Modified  : 2008-04-22
 */
class Mailer extends PHPMailer{
	
	function __construct(){
		$this->IsSMTP();
		$this->setting();
	}

	/**
	 * 判断是否是正确的email地址
	 *
	 * @param String $email
	 * @return Boolean
	 */
	function isEmail($email) {
		return ( strlen($email) > 5 
				&& preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $email));
	}
	
	function setCharset($charset = 'gb2312'){
		$this->CharSet = $charset;
	}
	
	function setEncoding($encoding = 'base64'){
		$this->Encoding = $encoding;
	}
	
	function setPort($port = 25){
		$this->Port = $port;
	}
	
	function setSmtpDebug($SMTPDebug = true){
		$this->SMTPDebug = $SMTPDebug;
	}
	
	function setSmtpSecure($secure='ssl', $port=null){
		$this->SMTPSecure = $secure;
		if (!is_null($port)) {
			$this->setPort($port);
		}
	}

	/**
	 * 是否使用ssl链接smtp服务器
	 *
	 * @param Integer $port
	 */
	function SetSsl($port=NULL){
		$this->setSmtpSecure('ssl', $port);
	}

	/**
	 * 设置发送邮件相关参数
	 *
	 * @param unknown_type $config
	 * @example $config = array (
		  'host' => 'smtp.gmail.com',
		  'port' => 465,
		  'auth' => true,
		  'ssl' => true,
		  'user' => '**@gmail.com',
		  'pass' => '**',
		  'from' => '',
		  'from_name' => '',
		  'word_wrap' => '',
		  'is_html' => true,
		)
		$Mailer->setting($config);
	 */
	function setting($config=null){
		if (is_null($config)) {
			$config = array(
				'host' => $GLOBALS['smtp_host'],
				'port' => $GLOBALS['smtp_port'],
				'auth' => true,
				'user' => $GLOBALS['smtp_user'],
				'pass' => $GLOBALS['smtp_pass'],
				'ssl'  => $GLOBALS['smtp_ssl'],
				'word_wrap' => $GLOBALS['mail_word_wrap'],
				'is_html'   => $GLOBALS['mail_is_html'],
			);
		}
		$this->Host	 = $config['host']; // SMTP servers
		$this->Port	 = $config['port']; // SMTP Port
		$this->SMTPAuth = $config['auth']; // turn on SMTP authentication
		$this->Username = $config['user']; // SMTP username
		$this->Password = $config['pass']; // SMTP password
		if ($config['ssl']) {
			$this->SetSsl();
		}
		if (!empty($config['from'])) {
			$this->From = $config['from'];
		}
		if (!empty($config['from_name'])) {
			$this->FromName = $config['from_name'];
		}
		$this->IsHTML($config['is_html']);
		$this->WordWrap = $config['word_wrap'];
		unset($config);
	}

	/**
	 * 设置邮件标题
	 *
	 * @param String $title
	 */
	function setTitle($title){
		$this->Subject = $title;
	}

	/**
	 * 设置邮件内容
	 *
	 * @param String $body
	 */
	function setBody($body){
		$this->Body = $body;
	}
}
